#include "thread.hpp"
#include <unistd.h>
#include <memory>
#include <vector>
#include "mutex.hpp"

int tickets = 10000;

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

class threadData
{
public:
    threadData(const std::string &threadname, pthread_mutex_t *mutex_p) : _threadname(threadname), _mutex_p(mutex_p)
    {
    }

    ~threadData()
    {
    }

public:
    std::string _threadname;
    pthread_mutex_t *_mutex_p;
};

void *get_ticket(void *args)
{
    std::string username = static_cast<const char*>(args);

    //threadData *td = static_cast<threadData *>(args);

    while (1)
    {
        // pthread_mutex_lock(&lock);//加锁
        // pthread_mutex_lock(td->_mutex_p);
        {
            //LockGuard lockguard(&lock);

            if (tickets > 0)
            {
                // pthread_mutex_lock(&lock);//加锁

                // pthread_mutex_lock(td->_mutex_p);

                usleep(120); // 用这段时间来模拟真实的要抢票时间

                // pthread_mutex_lock(td->_mutex_p);

                // pthread_mutex_lock(&lock);//加锁
                // 值得抢
                std::cout << username << "正在进行抢票" << tickets-- << std::endl;

                // pthread_mutex_unlock(&lock);//解锁
                // pthread_mutex_unlock(td->_mutex_p);
            }
            else
            {
                // pthread_mutex_unlock(&lock);//解锁
                // pthread_mutex_unlock(td->_mutex_p);
                break;
            }
        }

        usleep(10);
    }

    return nullptr;
}

int main()
{
// #define NUM 4
//     pthread_mutex_t lock;
//     pthread_mutex_init(&lock, nullptr);

//     std::vector<pthread_t> tids(NUM);
//     for (int i = 0; i < NUM; i++)
//     {
//         pthread_t t;
//         char buffer[64];
//         snprintf(buffer, sizeof(buffer), "thread-%d", i + 1);
//         threadData *td = new threadData(buffer, &lock);

//         pthread_create(&tids[i], nullptr, get_ticket, td);
//     }

//     for (const auto &tid : tids)
//     {
//         pthread_join(tid, nullptr);
//     }

    // pthread_t t1,t2,t3,t4;

    // pthread_create(&t1,nullptr,get_ticket,(void*)"pthread 1");
    // pthread_create(&t2,nullptr,get_ticket,(void*)"pthread 2");
    // pthread_create(&t3,nullptr,get_ticket,(void*)"pthread 3");
    // pthread_create(&t4,nullptr,get_ticket,(void*)"pthread 4");

    // pthread_join(t1,nullptr);
    // pthread_join(t2,nullptr);
    // pthread_join(t3,nullptr);
    // pthread_join(t4,nullptr);

    // pthread_mutex_destroy(&lock);

    // std::unique_ptr<Thread> thread0(new Thread(get_ticket,(void*)"user0",0));
    // std::unique_ptr<Thread> thread1(new Thread(get_ticket,(void*)"user1",1));
    // std::unique_ptr<Thread> thread2(new Thread(get_ticket,(void*)"user2",2));
    // std::unique_ptr<Thread> thread3(new Thread(get_ticket,(void*)"user3",3));
    // std::unique_ptr<Thread> thread4(new Thread(get_ticket,(void*)"user4",4));
    // std::unique_ptr<Thread> thread5(new Thread(get_ticket,(void*)"user5",5));
    // std::unique_ptr<Thread> thread6(new Thread(get_ticket,(void*)"user6",6));

    // thread0->join();
    // thread1->join();
    // thread2->join();
    // thread3->join();
    // thread4->join();
    // thread5->join();
    // thread6->join();

    Thread t0(get_ticket,(void*)"user0",0);
    Thread t1(get_ticket,(void*)"user1",1);
    Thread t2(get_ticket,(void*)"user2",2);
    Thread t3(get_ticket,(void*)"user3",3);

    t0.join();
    t1.join();
    t2.join();
    t3.join();


    return 0;
}
